package com.yuesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuesystem.common.R;
import com.yuesystem.dto.Link;
import com.yuesystem.dto.Point;
import com.yuesystem.entity.Academy;
import com.yuesystem.entity.AcademyItem;
import com.yuesystem.entity.FixedItem;
import com.yuesystem.service.AcademyItemService;
import com.yuesystem.service.AcademyService;
import com.yuesystem.service.FixedItemService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * 可视化控制类.
 */
@RestController
@RequestMapping("/visual")
@CrossOrigin
@Slf4j
public class VisualController {
  @Autowired
  private AcademyService academyService;
  
  @Autowired
  private FixedItemService fixedItemService;
  
  @Autowired
  private AcademyItemService academyItemService;
  
  /**
   * 省份-书院.
   */
  @GetMapping("/getLink1")
  public R<List<Link>> getLink1() {
    String[] provinces = {
        "北京", "天津", "河北", "山西", "内蒙古",
        "上海", "江苏", "浙江", "山东", "安徽",
        "辽宁", "吉林", "黑龙江",
        "湖北", "湖南", "河南", "江西",
        "广东", "广西", "海南", "福建", "香港", "澳门",
        "四川", "重庆", "贵州", "云南", "西藏",
        "陕西", "甘肃", "新疆", "青海", "宁夏", "台湾"
    };
    List<Link> list = new ArrayList<>();
    for (int i = 0; i < provinces.length; i++) {
      LambdaQueryWrapper<FixedItem> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(FixedItem::getRelation, "位于")
          .likeRight(FixedItem::getObject, provinces[i]);
      List<FixedItem> items = fixedItemService.list(queryWrapper);
      int finalI = i;
      List<Link> links = items.stream().map((item) -> {
        Link link = new Link();
        link.setSource(provinces[finalI]);
        link.setTarget(String.valueOf(item.getAcademyId()));
        return link;
      }).collect(Collectors.toList());
      list.addAll(links);
    }
    return R.success(list);
  }
  
  /**
   * 书院节点.
   */
  @GetMapping("/getPoint1")
  public R<List<Point>> getPoint1() {
    List<String> areas = Arrays.asList("华北地区", "华东地区", "东北地区", "华中地区",
        "华南地区", "西南地区", "西北地区");
    String[][] provinces = {
        {"北京", "天津", "河北", "山西", "内蒙古"},
        {"上海", "江苏", "浙江", "山东", "安徽", "江西", "福建", "台湾"},
        {"辽宁", "吉林", "黑龙江"},
        {"湖北", "湖南", "河南"},
        {"广东", "广西", "海南", "香港", "澳门"},
        {"四川", "重庆", "贵州", "云南", "西藏"},
        {"陕西", "甘肃", "新疆", "青海", "宁夏"}
    };
    LambdaQueryWrapper<FixedItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FixedItem::getRelation, "位于");
    List<FixedItem> list = fixedItemService.list(queryWrapper);
    List<Point> points = list.stream().map((item) -> {
      Point point = new Point();
      point.setSymbolSize(20);
      point.setName(item.getSubject());
      point.setId(item.getAcademyId());
      
      String place = item.getObject();
      String area = "";
      for (int i = 0; i < 7; i++) {
        for (int j = 0; j < provinces[i].length; j++) {
          if (place.contains(provinces[i][j])) {
            area = areas.get(i);
            break;
          }
        }
      }
      point.setCategory(area);
      return point;
    }).collect(Collectors.toList());
    return R.success(points);
  }
  
  /**
   * 客体节点.
   *
   * @return 节点
   */
  @GetMapping("/getPoint2")
  public R<List<Point>> getPoint2() {
    List<FixedItem> list = fixedItemService.list();
    List<Point> points = list.stream().map((item) -> {
      Point point = new Point();
      point.setId((item.getId() + 2000));
      point.setName(item.getObject());
      point.setSymbolSize(20);
      
      return point;
    }).collect(Collectors.toList());
    return R.success(points);
  }
  
  
  /**
   * 根据书院id查找节点.
   *
   * @param id 书院id
   * @return 书院节点
   */
  @GetMapping("/getAcademyPoint/{id}")
  public R<List<Point>> getAcademyPoint(@PathVariable Long id) {
    //增加书院主体节点
    Academy academy = academyService.getById(id);
    
    Point p1 = new Point();
    p1.setId(id);
    p1.setSymbolSize(60);
    p1.setName(academy.getAcademyName());
    List<Point> list = new ArrayList<>();
    list.add(p1);
    
    //增加书院客体节点
    LambdaQueryWrapper<AcademyItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(AcademyItem::getAcademyId, id)
        .eq(AcademyItem::getIsDeleted, 0);
    List<AcademyItem> items = academyItemService.list(queryWrapper);
    List<Point> p2 = items.stream().map((item) -> {
      Point point = new Point();
      point.setId(2000 + item.getId());
      point.setSymbolSize(30);
      point.setName(item.getObject() != null ? item.getObject() : "客体未详");
      return point;
    }).collect(Collectors.toList());
    
    //结合
    list.addAll(p2);
    return R.success(list);
  }
  
  
  /**
   * 获得单个书院的联系.
   *
   * @param id 书院id
   * @return 该书院的联系
   */
  @GetMapping("/getAcademyLink/{id}")
  public R<List<Link>> getAcademyLink(@PathVariable Long id) {
    LambdaQueryWrapper<AcademyItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(AcademyItem::getAcademyId, id)
        .eq(AcademyItem::getIsDeleted, 0);
    List<AcademyItem> items = academyItemService.list(queryWrapper);
    List<Link> list = items.stream().map((item) -> {
      Link link = new Link();
      link.setSource(String.valueOf(id));
      link.setTarget(String.valueOf(item.getId() + 2000));
      link.setValue(item.getRelation());
      return link;
    }).collect(Collectors.toList());
    return R.success(list);
  }

}
